﻿Public Class RarOrgTypol
    Dim db As New Connexion
    Dim MethTypologieNiv1CRUD As TypologieNiv1CRUD
    Dim action As String
    Dim Entrer As String
    Dim TextBoxID As String
    Public Table As String
    Public databaseName As String


    Sub New(ByVal databaseStr As String)
        InitializeComponent()
        databaseName = databaseStr
    End Sub

    Private Sub RarOrgTypol_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Entrer = ""

        DataGridViewTypol.MultiSelect = False
        DataGridViewTypol.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridViewTypol.ReadOnly = True
        miseajour()

    End Sub

    Private Sub miseajour()
        db.myReq = "SELECT * FROM  [dbo].[" & Table & "]"
        db.Connecter(databaseName)
        DataGridViewTypol.Rows.Clear()
        While db.rdr.Read()
            DataGridViewTypol.Rows.Add(db.rdr(0), db.rdr(1), db.rdr(2), db.rdr(3))
        End While
        db.Disconnecter()

        If DataGridViewTypol.RowCount - 1 = 0 Then
            Me.TextBoxCodeTypol.Text = ""
            Me.TextBoxNomTypol.Text = ""
            Me.TextBoxAbrv.Text = ""
            Me.TextBoxSW.Text = ""
        Else
            DataGridViewTypol.Rows(0).Selected = True
            Me.TextBoxCodeTypol.Text = DataGridViewTypol.SelectedCells(1).Value
            Me.TextBoxNomTypol.Text = DataGridViewTypol.SelectedCells(2).Value
            Me.TextBoxAbrv.Text = DataGridViewTypol.SelectedCells(3).Value
            Me.TextBoxSW.Text = DataGridViewTypol.SelectedCells(4).Value
            TextBoxID = DataGridViewTypol.SelectedCells(0).Value
        End If
    End Sub

    Private Sub DataGridView3_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridViewTypol.CurrentCellChanged
        If Entrer = "debut" Then
            action = ""
            Selection()
        End If
    End Sub
    Private Sub DataGridView3_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridViewTypol.MouseClick
        If e.Button = MouseButtons.Left Then
            Entrer = "debut"
            action = ""
            Selection()
        End If
    End Sub
    Private Sub Selection()
        If DataGridViewTypol.RowCount - 1 = 0 Then
            Me.TextBoxCodeTypol.Text = ""
            Me.TextBoxNomTypol.Text = ""
            Me.TextBoxAbrv.Text = ""
            Me.TextBoxSW.Text = ""
        Else
            Me.TextBoxCodeTypol.Text = DataGridViewTypol.SelectedCells(1).Value
            Me.TextBoxNomTypol.Text = DataGridViewTypol.SelectedCells(2).Value
            Me.TextBoxAbrv.Text = DataGridViewTypol.SelectedCells(3).Value
            Me.TextBoxSW.Text = DataGridViewTypol.SelectedCells(4).Value
            TextBoxID = DataGridViewTypol.SelectedCells(0).Value
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Dispose()
    End Sub

    Private Sub ButtonNouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNouv.Click
        action = "ajouter"
        Me.TextBoxCodeTypol.Text = ""
        Me.TextBoxNomTypol.Text = ""
        Me.TextBoxAbrv.Text = ""
        Me.TextBoxSW.Text = ""
    End Sub

    Private Sub ButtonSup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSup.Click
        Dim requete As String = ""
        Entrer = ""
        action = ""
        db.myReq = "SELECT Top 1 syscolumns.name AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= '" & Table & "'"
        db.Connecter(databaseName)
        While db.rdr.Read()
            requete = "DELETE FROM  [dbo].[RARORGTYPOL] WHERE [Id_RarOrgTypol]='" & TextBoxID & "'"
            MsgBox(requete)
        End While
        db.Disconnecter()

        db.myReq = requete
        db.Connecter(databaseName)
        db.Disconnecter()
        miseajour()
    End Sub

    Private Sub ButtonAjouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.TextBoxCodeTypol.Text = ""
        Me.TextBoxNomTypol.Text = ""
        Me.TextBoxAbrv.Text = ""
        Me.TextBoxSW.Text = ""
        action = "ajouter"
    End Sub


    Private Sub ButtonEnregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEnregistrer.Click
        Entrer = ""
        Dim requete As String = ""
        db.myReq = "SELECT syscolumns.name AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= '" & Table & "'"
        db.Connecter(databaseName)

        Dim tab As String() = New String(4) {}
        Dim i As Integer = 0
        While db.rdr.Read()
            tab(i) = db.rdr(0)
            i = i + 1
        End While

        If action = "ajouter" Then
            requete = "INSERT INTO  [dbo].[RARORGTYPOL]([COD_RARTPL],[Nom_RarOrgTypol],[ABREV_RARTPL],[SWEB_RARTPL])VALUES('" & Me.TextBoxCodeTypol.Text & "','" & Me.TextBoxNomTypol.Text & "','" & Me.TextBoxAbrv.Text & "','" & Me.TextBoxSW.Text & "')"
        Else
            requete = "Update  [dbo].[RARORGTYPOL] SET [COD_RARTPL] = '" & Me.TextBoxCodeTypol.Text & "', [Nom_RarOrgTypol] = '" & Me.TextBoxNomTypol.Text & "',[ABREV_RARTPL] = '" & Me.TextBoxAbrv.Text & "', [SWEB_RARTPL]='" & Me.TextBoxSW.Text & "' WHERE [Id_RarOrgTypol]='" & TextBoxID & "'"
        End If
        MsgBox(requete)
        db.Disconnecter()


        db.myReq = requete
        db.Connecter(databaseName)
        db.Disconnecter()
        miseajour()

    End Sub
End Class